package com.kzd.sdk.ui.fragment;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kzd.sdk.base.OwnerFragment;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnAccessListener;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.ui.activity.KZD_UserServiceActivity;
import com.kzd.sdk.utils.AppUtils;

public class KZD_AccessForgetFragment extends OwnerFragment {

    private TextView tvAccessForgetSubmit, tvAccessForgetCodeGet;
    private EditText etAccessForgetMobile, etAccessForgetCode, etAccessForgetPass;

    private String mSessionId = "";

    private final CountDownTimer mCountDownTimer = new CountDownTimer(1000 * 60, 1000) {
        public void onTick(long millisUntilFinished) {
            String text = "剩余" + millisUntilFinished / 1000 + "秒";
            tvAccessForgetCodeGet.setText(text);
        }

        public void onFinish() {
            tvAccessForgetCodeGet.setTextColor(Color.parseColor("#2983FF"));
            tvAccessForgetCodeGet.setEnabled(true);
            tvAccessForgetCodeGet.setText("获取验证码");
        }
    };

    private final KZD_HttpCallback<String> callbackPhoneCode = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            mSessionId = data;
            mCountDownTimer.start();
            tvAccessForgetCodeGet.setTextColor(Color.parseColor("#999999"));
            tvAccessForgetCodeGet.setEnabled(false);
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    public KZD_OnAccessListener mOnAccessListener;

    public void setOnAccessListener(KZD_OnAccessListener onAccessListener) {
        this.mOnAccessListener = onAccessListener;
    }

    @Override
    public String getLayoutIdByName() {
        return "kzd_fragment_access_forget";
    }

    @Override
    public void initView(View view) {

        tvAccessForgetCodeGet = view.findViewById(AppUtils.getId(mContext, "tvAccessForgetCodeGet"));
        etAccessForgetMobile = view.findViewById(AppUtils.getId(mContext, "etAccessForgetMobile"));
        etAccessForgetCode = view.findViewById(AppUtils.getId(mContext, "etAccessForgetCode"));
        etAccessForgetPass = view.findViewById(AppUtils.getId(mContext, "etAccessForgetPass"));

        tvAccessForgetSubmit = view.findViewById(AppUtils.getId(mContext, "tvAccessForgetSubmit"));

        tvAccessForgetCodeGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etAccessForgetMobile.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    toast("请输入手机号");
                } else {
                    KZD_ApiManager.httpPhoneCode(phone, 1, callbackPhoneCode);
                }
            }
        });

        //确定修改密码点击事件
        tvAccessForgetSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitUpdatePassword();
            }
        });

        view.findViewById(AppUtils.getId(mContext, "ivAccessForgetBack"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnClickListener != null) {
                            mOnClickListener.onClick(v);
                        }
                    }
                });
        view.findViewById(AppUtils.getId(mContext, "ivAccessForgetService"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_UserServiceActivity.open(mContext);
                    }
                });
    }

    /**
     * 提交修改密码
     */
    private void submitUpdatePassword() {
        if (TextUtils.isEmpty(etAccessForgetMobile.getText())) {
            Toast.makeText(mContext, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(etAccessForgetCode.getText())) {
            Toast.makeText(mContext, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(etAccessForgetPass.getText())) {
            Toast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (mOnAccessListener == null) {
            return;
        }
        mOnAccessListener.onForget(etAccessForgetMobile.getText().toString(),
                etAccessForgetCode.getText().toString(), mSessionId, etAccessForgetPass.getText().toString());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mCountDownTimer.cancel();
    }
}
